package com.shop2cn.iapi.delivery.testcase;

import java.math.BigDecimal;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.parameter.enums.LogisticsTypeEnum;
import com.shop2cn.iapi.sqoptrade.parameter.utill.Utils;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.shop2cn.iapi.delivery.parameter.DeliveryInfo;
import com.shop2cn.iapi.delivery.parameter.ModifySqDeliveryBillNoReq;
import com.shop2cn.iapi.delivery.service.ModifySqDeliveryBillNoCall;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AccountCallServiceV2;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.database.sharding.db.ext.model.DeliveryPo;
import com.ymttest.database.sharding.db.sqlwapper.OrderWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;

public class Ts_modifySqDeliveryBillNo {

	private static ModifySqDeliveryBillNoReq req;
	private static ModifySqDeliveryBillNoCall call;

	private static OrderWapper orderWapper = new OrderWapper();

	private static long autotest_goodsOwnerId = Long.valueOf(EnvSetup
			.getData("autotest_goodsOwnerId"));

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("校验物流单号接口");
	}

	@Before
	public void caseUp() {
		req = new ModifySqDeliveryBillNoReq();
		call = new ModifySqDeliveryBillNoCall();

		String accountid1;
		try {
			accountid1 = AccountCallServiceV2.getAccountId(20238699);
			AccountCallServiceV2.resetAccount(accountid1, new BigDecimal(
					9000000), new BigDecimal(0));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(call.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	/**
	 * 校验 mysql delivery 表
	 * 
	 * @param info
	 */
	private void verifyMysqlDb(DeliveryInfo info, long sellerId,
			int logisticsType) {

		long orderId = info.getOrderId();

		DeliveryPo actualDeliveryPo = orderWapper
				.selectDeliveryByOrderId(orderId);
		int deliveryPoSize = orderWapper.selectDeliverySizeByOrderId(orderId);

		Logger.comment("校验 delivery 表数据");

		Logger.verifyEquals(info.getYmtExpressCode(),
				actualDeliveryPo.getYmtExpressCode(), "验证数据库YmtExpressCode");

		Logger.verifyEquals(false, actualDeliveryPo.getOfficalLogistics(),
				"验证数据库OfficalLogistics");

		Logger.verifyEquals(Utils.zeroIfNull(info.getRealDeliveryArea()),
				actualDeliveryPo.getRealDeliveryArea(), "验证数据库RealDeliveryArea");

		Logger.verifyEquals(orderId, actualDeliveryPo.getOrderId(),
				"验证数据库OrderId");

		Logger.verifyEquals(sellerId, actualDeliveryPo.getSellerId(),
				"验证数据库SellerId");

		Logger.verifyEquals(logisticsType, actualDeliveryPo.getLogisticsType(),
				"验证数据库LogisticsType");

		Logger.verifyEquals(false, actualDeliveryPo.getDeleted(),
				"验证数据库Deleted");

		Logger.verifyEquals(info.getDeliverySource(),
				actualDeliveryPo.getSource(), "验证数据库Source");

		if (logisticsType == LogisticsTypeEnum.PackageDelivery.getCode()
				&& info.getDeliverySection() == 3 && deliveryPoSize == 1)
			Logger.verifyEquals(true, actualDeliveryPo.getSourceTrack(),
					"验证数据库SourceTrack");
		else
			Logger.verifyEquals(false, actualDeliveryPo.getSourceTrack(),
					"验证数据库SourceTrack");

		Logger.verifyEquals(info.getBillNo(), actualDeliveryPo.getBillNo(),
				"验证数据库BillNo");

		Logger.verifyEquals(info.getMobile(), actualDeliveryPo.getMobile(),
				"验证数据库Mobile");

		Logger.verifyEquals(info.getDeliverySection(),
				actualDeliveryPo.getSection(), "验证数据库Section");
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_001() {
		Logger.start(true, "神器订单 发货之后,修改物流单号成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			PayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			String billNo = SqTradingCallService.delivery(
					autotest_goodsOwnerId, orderId, 1L, null);

			Thread.sleep(10000);

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(orderId);
			req.setOldBillNo(billNo);
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			DeliveryInfo deliverInfo = new DeliveryInfo();
			deliverInfo.setOrderId(orderId);
			deliverInfo.setBillNo(req.getBillNo());
			deliverInfo.setYmtExpressCode(req.getYmtExpressCode());
			deliverInfo.setDeliverySource(1);
			deliverInfo.setDeliverySection(3);

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "true",
					"校验IsSuccess");

			verifyMysqlDb(deliverInfo, autotest_goodsOwnerId, 999);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_002() {
		Logger.start(true, "神器订单 发货之后,修改物流公司和物流单号成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			PayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			String billNo = SqTradingCallService.delivery(
					autotest_goodsOwnerId, orderId, 1L, null);

			Thread.sleep(10000);

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(orderId);
			req.setOldBillNo(billNo);
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			DeliveryInfo deliverInfo = new DeliveryInfo();
			deliverInfo.setOrderId(orderId);
			deliverInfo.setBillNo(req.getBillNo());
			deliverInfo.setYmtExpressCode(req.getYmtExpressCode());
			deliverInfo.setDeliverySource(1);
			deliverInfo.setDeliverySection(3);

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "true",
					"校验IsSuccess");

			verifyMysqlDb(deliverInfo, autotest_goodsOwnerId, 999);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_003() {
		Logger.start(true, "神器订单 发货之后,不传入orderId修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			// req.setOrderId(orderId);
			req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("订单记录不存在!", call.getMsg(), "校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_004() {
		Logger.start(true, "神器订单 发货之后,不传入oldYmtExpressCode修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			// req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("oldBillNo:面单号未提供", call.getMsg(), "校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_005() {
		Logger.start(true, "神器订单 发货之后,不传入ymtExpressName修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			// req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("ymtExpressName:物流公司名称未提供", call.getMsg(),
					"校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_006() {
		Logger.start(true, "神器订单 发货之后,不传入oldYmtExpressName修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			// req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("oldYmtExpressName:原物流公司名称未提供", call.getMsg(),
					"校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_007() {
		Logger.start(true, "神器订单 发货之后,不传入billNo修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			req.setOldBillNo("8482734287819");
			// req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("billNo:面单号未提供", call.getMsg(), "校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_008() {
		Logger.start(true, "神器订单 发货之后,不传入oldBillNo修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			// req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("oldBillNo:面单号未提供", call.getMsg(), "校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_009() {
		Logger.start(true, "神器订单 发货之后,不传入goodsOwnerId修改物流单号失败");
		try {

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(6630608699L);
			req.setOldBillNo("8482734287819");
			req.setBillNo(TestDataManager.getRandomNum(13));
			req.setYmtExpressCode("Y037");
			// req.setGoodsOwnerId(autotest_goodsOwnerId);
			req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "false",
					"校验IsSuccess");
			Logger.verifyEquals("goodsOwnerId:买手ID未提供", call.getMsg(), "校验msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({ P1.class })
	@TestCase
	public void Tc_modifySqDeliveryBillNo_010() {
		Logger.start(true, "神器订单 发货之后,不传入cdId和ymtExpressCode，修改物流公司和物流单号成功");
		try {

			// 创建订单
			Logger.comment("前置数据准备");

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			Long userId = placeOrderResp.getUserId();

			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			PayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService
					.sellerAcceptOrder(autotest_goodsOwnerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			String billNo = SqTradingCallService.delivery(
					autotest_goodsOwnerId, orderId, 1L, null);

			Thread.sleep(10000);

			req.setOldYmtExpressCode("Y013");
			req.setOrderId(orderId);
			req.setOldBillNo(billNo);
			req.setBillNo(TestDataManager.getRandomNum(13));
			// req.setYmtExpressCode("Y037");
			req.setGoodsOwnerId(autotest_goodsOwnerId);
			// req.setCdId("12345678");
			req.setOldYmtExpressName("EMS(中国件)");
			req.setYmtExpressName("圆通速递-国际件");

			call.setData(req);
			call.callService();

			DeliveryInfo deliverInfo = new DeliveryInfo();
			deliverInfo.setOrderId(orderId);
			deliverInfo.setBillNo(req.getBillNo());
			deliverInfo
					.setYmtExpressCode(req.getYmtExpressCode() == null ? "Y000"
							: req.getYmtExpressCode());
			deliverInfo.setDeliverySource(1);
			deliverInfo.setDeliverySection(3);

			Logger.verifyEquals(call.getData().optString("IsSuccess"), "true",
					"校验IsSuccess");

			verifyMysqlDb(deliverInfo, autotest_goodsOwnerId, 999);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
